Connect with
 
In android I recently had a task that involved converting a date string into a date object.i have many time used this type of conversion when we are used Date class and display it.
In this post i post the both conversion and many other combination.

String to Date useing Date Class:
String dtStart = "2012-10-15";  
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");  
try{  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
      e.printStackTrace();  
}


Date to String useing Date Class:
SimpleDateFormat  dformat= new SimpleDateFormat("yyyy-MM-dd");  
try {  
  Date date = new Date();  
 String datetime = dformat.format(date);
 System.out.println("Current Date Time : " + datetime);
} catch (ParseException e) {  
     e.printStackTrace();  
}


Now how to add any integer value in current/passed Date and used them.
you can add date two way..

(1)first is using Calander class.
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, 3);  // number of days to add
String dt = sdf.format(c.getTime());  // dt is now the new date








 
In Android, I am facing many time convert Bitmap to String and String to Bitmap. When send or receive Bitmap to server and storing Image in database.

Bitmap to String:
public String BitMapToString(Bitmap bitmap){
            ByteArrayOutputStream baos=new  ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
            byte [] arr=baos.toByteArray();
            String result=Base64.encodeToString(arr, Base64.DEFAULT);
            return result;
      }


String to Bitmap:
public Bitmap StringToBitMap(String image){
       try{
           byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
           Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
           return bitmap;
         }catch(Exception e){
           e.getMessage();
          return null;
         }
 }

    Author

    "IF you you don't have time  for fitness  then save time for sickness tomorrow"

    Android Tutorial

    Archives

    October 2012

    Categories

    All
    Date Conversation
    String To Bitmap And Vice Versa